$(document).ready(function(){
    var elem = document.getElementById("option2");
    elem.setAttribute("value",ordinaryId);
    elem = document.getElementById("option3");
    elem.setAttribute("value",VIPId);
    elem = document.getElementById("option1");
    elem.setAttribute("value",anyId);
	$('#results').hide();
	$('#right-preloader').hide();
});


function performQuery()
{
	$('#right-preloader').show();
	$('#results-count').empty();
	$("#grant-vip").hide();
	$("#revoke-vip").hide();
	getUsersCount();
    var totalCount = usersCount;
    var userTable = $('#result-table');
    userTable.empty();
	userTable.hide();
	if (totalCount > 0)
	{
		var rowstring = '<thead><tr id="result-head">';
		rowstring+='<th><input type="checkbox" id="main_checkbox" onClick="javascript:MainCheckBox()"></th>';
		rowstring+='<th>'+ "Login" +'</th>';
		rowstring+='<th>'+ "Name" +'</th>';
		rowstring+='<th>'+ "Surname" +'</th>';
		rowstring+='<th>'+ "Status" +'</th>';
		rowstring+='</tr></thead>';
		userTable.append(rowstring);
		var elem = document.getElementById('result-head');
		elem.style.background = 'white';
		var VIPCustomerId = VIPId;
		pager = new Pager('result_body', 10, 'pager', 'pageNavPosition');
		pager.init();
		pager.showPage(1);
		$("#grant-vip").show();
		$("#revoke-vip").show();
		$('#results-count').append(totalCount + " users found.");
	}
	else
	{
		$('#results-count').append("no users found.");
	}
	$('#results').show();
	$('#right-preloader').hide();
	userTable.show();
    //TODO: pagination  (data.count is total entries count)
}

function grantStatus()
{
    var array = document.getElementsByName("rat");
    var res = [];
    var k = 0;
    $.each(array, function(index, value) {
        var checkboxId = "ch" + value.id;
        if ($('#'+checkboxId).prop('checked') == true)
        {
            res[k++] = value.id;
        }
    });
    doGrantVIPStatus(res);
}

function revokeStatus()
{
    var array = document.getElementsByName("rat");
    var res = [];
    var k = 0;
    $.each(array, function(index, value) {
        var checkboxId = "ch" + value.id;
        if ($('#'+checkboxId).prop('checked') == true)
        {
            res[k++] = value.id;
        }
    });
    doRevokeVIPStatus(res);
}

function getNeededUsers(page, itemsPerPage)
{
    $.ajax(
        {
            type: 'POST',
            async: false,
            url: 'dashboard',
            data:
            {
                action: "getUsersList",
                login : $('#login').val(),
                name : $('#name').val(),
                surname : $('#surname').val(),
                usergroup : $('#group').val(),
                pageNumber: page,
                itemsPerPage: itemsPerPage
            },
        success:
        function(data)
        {
			data = $.parseJSON(data);
            recordsToBeShown = data;
        }});
}

function getUsersCount()
{
	$.ajax(
    {
        url: 'dashboard',
        type: 'POST',
        async: false,
        data:
        {
            action: "getUsersCount",
            login : $('#login').val(),
            name : $('#name').val(),
            surname : $('#surname').val(),
            usergroup : $('#group').val()
        },
        success:
        function(data)
        {
            usersCount = data;
        }
    });
}

function doGrantVIPStatus(ids)
{
    $.post('dashboard',
        {
            action: "grantVIPStatus",
            ids: ids
        },
        function(data)
        {
            data = $.parseJSON(data);
            if (data != "")
            {
                for (var i = 0; i < data.length; i++)
                {
                    var id = "st" + data[i];
                    document.getElementById(id).innerHTML = "VIP";
                }
            }
        });
}

function doRevokeVIPStatus(ids)
{
    $.post('dashboard',
        {
            action: "revokeVIPStatus",
            ids: ids
        },
        function(data)
        {
            data = $.parseJSON(data);
            if (data != "")
            {
                for (var i = 0; i < data.length; i++)
                {
                    var id = "st" + data[i];
                    document.getElementById(id).innerHTML = "Basic";
                }
            }
        });
}

function updateMainCheckBox()
{
	var checkboxes = document.getElementsByName("checkboxes");
	var on = 0, total = checkboxes.length;
	$.each(checkboxes, function(index, element) {
		if ($(element).prop('checked') == true)
			++on;
	});
	var mainCheckbox = $('#main_checkbox');
	if (on == total)
	{
		mainCheckbox.css('opacity','1.0');
		mainCheckbox.attr('checked','true');
	}
	else if (on == 0)
	{
		mainCheckbox.css('opacity','1.0');
		mainCheckbox.removeAttr('checked');
	}
	else
	{
		mainCheckbox.css('opacity','0.5');
		mainCheckbox.attr('checked','true');
	}
}

function MainCheckBox()
{
	var mainCheckbox = $('#main_checkbox');
	var result;
	if (mainCheckbox.prop('checked') == true || mainCheckbox.css('opacity') == '0.5')
	{
		result = true; 
		mainCheckbox.css('opacity','1.0');
		mainCheckbox.attr('checked', 'true');
	}
	else
	{
		result = false;
		mainCheckbox.removeAttr('checked');	
	}
	$.each(checkboxes, function(index, element) {
		if (result == true)
			$(element).attr('checked','true');
		else
			$(element).removeAttr('checked');
	});
}